<?php

/**
 * 
 *
 * @version $Id: class_sindex.inc.php,v 1.2 2004/08/21 13:43:25 phppc Exp $
 * @copyright 2004 
 **/

/**
 * nc_search_index
 * 
 * @package 
 * @author Armin
 * @copyright Copyright (c) 2004
 * @version $Id: class_sindex.inc.php,v 1.2 2004/08/21 13:43:25 phppc Exp $
 * @access public
 **/
class nc_search_index {
	/**
	 * nc_search_index::prepare_string()
	 * 
	 * @param $str
	 * @return 
	 **/
	function prepare_string($str) {
		$search[1] = "\n";
		$search[2] = "!";
		$search[3] = ",";
		$search[4] = ".";
		$search[5] = "(";
		$search[6] = ")";
		$replace[1] = " ";
		$replace[2] = " ";
		$replace[3] = " ";
		$replace[4] = " ";
		$replace[5] = "";
		$replace[6] = "";
		$anzahl = count($search);
		for($nr = 1;$nr <= $anzahl;$nr++) {
			$str = str_replace($search[$nr],$replace[$nr],$str);
		}
		$suchen = array("!\[.*\]!i");
		$ersetzen = array(" ");
		return preg_replace($suchen,$ersetzen,$str);
	}
	/**
	 * nc_search_index::insert_index()
	 * 
	 * @param $news_id
	 * @param integer $ort
	 * @param string $betreff
	 * @param string $text
	 * @param integer $delete
	 * @return 
	 **/
	function insert_index($news_id,$ort = 1,$betreff = "",$text = "",$delete = 1) {
		global $dbnc,$nc;
		if ($betreff == "" AND $text == "") list($betreff,$text) = $this->db_abfrage("SELECT n_title,n_text FROM ".$this->config["prefix"]."main WHERE news_id=\"$news_id\"");
		$betreff_array = explode(" ",$betreff);
		if($delete == 1) $this->db_abfrage_q("DELETE FROM ".$this->config["prefix"]."sindex WHERE news_id=\"$news_id\"");
		foreach ($betreff_array as $u => $b) {
			$b = rtrim($b);
			if($b != "" AND $b != " ") $this->db_abfrage_q("INSERT ".$this->config["prefix"]."sindex (news_id,content,ort) VALUES (\"$news_id\",\"$b\",\"1\")");
		}
		if ($ort== 2) {
			$text = $this->prepare_string($text);
			$text_array = explode(" ",$text);
		    foreach ($text_array as $u => $t) {
				$t = rtrim($t);
				if($t != "" AND $t != " ") $this->db_abfrage_q("INSERT ".$this->config["prefix"]."sindex (news_id,content,ort) VALUES (\"$news_id\",\"$t\",\"2\")");
			}
		}
	}
	/**
	 * nc_search_index::resync_index()
	 * 
	 * @return 
	 **/
	function resync_index() {
		global $dbnc,$nc;
		$this->db_abfrage_q("DELETE FROM ".$this->config["prefix"]."sindex");
		$erg = $this->db_abfrage_q("SELECT news_id,n_title,n_text FROM ".$this->config["prefix"]."main");
		while($news = $this->db_abfrage_fetch($erg)){
			$this->insert_index($news["news_id"], 2, $news["n_title"], $news["n_text"], 0);
		} // while
	}
	/**
	 * nc_search_index::search_content()
	 * 
	 * @param $content
	 * @param integer $ort
	 * @param integer $type
	 * @return 
	 **/
	function search_content($content,$ort = 1,$type = 1) {
	global $dbnc,$nc;
	$content = str_replace("*","%",$content);
	if($type == 2) $zwischen = "OR";
	elseif ($type == 1) $zwischen = "AND";
	$content_array = explode(" ",$content);
	$anzahl = count($content_array);
	$start = 1;
	$t = " AND (".$this->config["prefix"]."sindex.ort=\"1\"";
	if($ort == 2) $t .= " OR ".$this->config["prefix"]."sindex.ort=\"2\"";
	$t .= ")";
	foreach ($content_array as $teil => $inhalt) {
		$abfrage .= "(" . $this->config["prefix"]. "sindex.content LIKE \"" . $inhalt . "\" $t)";
		if($start < $anzahl) $abfrage .= " " . "OR" . " ";
		$start++;
	}
	$erg = $this->db_abfrage_q("SELECT news_id FROM ".$this->config["prefix"]."sindex WHERE " . $abfrage);
	unset($abfrage);
	$anzahl_ids = 0;
	while(list($news_id) = $this->db_abfrage_fetch_q($erg)){
		if($ids[$news_id] != 1) {
			$ids[$news_id] = 1;
			$anzahl_ids++;
		}
	} // while
	if ($type == 1 AND $start > 2) {
		$start = 0;
	    foreach($ids as $news_id => $content) {
			$and .= "news_id = \"$news_id\"";
			$start++;
			if($start < $anzahl_ids) $and .= " OR ";
		}
		unset($ids,$anzahl_ids);
		$ids = array();
		$erg = $this->db_abfrage_q("SELECT news_id,n_title,n_text FROM ".$this->config["prefix"]."main WHERE $and");
		while(list($news_id,$n_title,$n_text) = $this->db_abfrage_fetch_q($erg)){
			unset($valid_result);
			foreach ($content_array as $teil => $inhalt) {
				if(substr_count($n_title,$inhalt) >= 1) $valid_result = 1;
				else $valid_result = 0;
				if($ort == 2){
					if($substr_count($n_text,$inhalt) >= 1) $valid_result = 1;
				}
				if($valid_result != 1) break;
			}
			if ($valid_result == 1) {
			    $anzahl_ids++;
				$ids[$news_id] = 1;
			}
		} // while
	}
	if($anzahl_ids >= 1) {
		$start = 0;
		foreach($ids as $news_id => $content) {
		$abfrage .= $this->config["prefix"] . "main.news_id = \"$news_id\"";
		$start++;
		if($start < $anzahl_ids) $abfrage .= " OR ";
		}
	}else $abfrage = $this->config["prefix"] . "main.n_title = \"$content\"";
	return "AND($abfrage)";
	}
}

$nc_sindex = new nc_search_index;

?>